package com.core.life.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.core.life.dao.IImageDao;
import com.core.life.entity.ConsumeRecordEntity;
import com.core.life.entity.ImageEntity;
import com.core.life.service.IImageService;
import com.core.life.vo.ImageVo;
import com.utils.UUIDGeneratorUtil;
import com.utils.config.CommonConfig;
import com.utils.date.DateUtil;
import com.utils.file.FileUtil;

@Service
public class IImageServiceImpl implements IImageService{

	@Resource
	private IImageDao imageDao;
	
	@Transactional
	@Override
	public void saveImage(MultipartFile img, ConsumeRecordEntity consumeRecord) throws Exception {
		if(img != null && img.getSize() > 0){
			ImageEntity image = new ImageEntity();
			String imgFileName = img.getOriginalFilename();
			//保存img文件
			String webPath = CommonConfig.getWebPath();
			if(StringUtils.isNotBlank(webPath)){
				String imgFilePath = "static" + File.separator + "upload" + 
												File.separator + DateUtil.format(new Date(), "yyyy-MM-dd") +
												File.separator + UUIDGeneratorUtil.getUUID();
				String absPath = webPath + File.separator + imgFilePath;
				FileUtil.saveFile(absPath, imgFileName, img.getInputStream());
				imgFilePath = imgFilePath + File.separator + imgFileName;
				image.setRelativePath(imgFilePath.replace("\\", "/"));
				image.setAbsolutePath(absPath + File.separator + imgFileName);
				image.setName(imgFileName);
				image.setConsumeRecordId(consumeRecord.getId());
				imageDao.save(image);
			}
		}
	}
	
	@Transactional
	@Override
	public void updateImage(MultipartFile img, ConsumeRecordEntity consumeRecord) throws Exception {
		List<Long> ids = new ArrayList<Long>();
		ids.add(consumeRecord.getId());
		//先删除，再保存
		this.deleteImagesByConsumeRecordIds(ids);
		this.saveImage(img, consumeRecord);
		
	}

	@Override
	public List<ImageVo> getImageListByConsumeRecordId(Long consumeCountId) {
		List<ImageEntity> list = imageDao.findByParam("consumeRecordId", consumeCountId);
		if(CollectionUtils.isNotEmpty(list)){
			List<ImageVo> imgVoList = new ArrayList<ImageVo>(list.size());
			ImageVo imgVo = null;
			for (ImageEntity imageEntity : list) {
				imgVo = new ImageVo();
				imgVo.setId(imageEntity.getId());
				imgVo.setName(imageEntity.getName());
				imgVo.setRelativePath(imageEntity.getRelativePath());
				imgVo.setAbsolutePath(imageEntity.getAbsolutePath());
				imgVoList.add(imgVo);
			}
			return imgVoList;
		}
		return null;
	}

	@Transactional
	@Override
	public void deleteImagesByConsumeRecordIds(List<Long> consumeRecordIds){
		imageDao.deleteByConsumeCountIds(consumeRecordIds);
	}

}
